package com.jxwifi.cloud.quickclean.toolkit;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.idroid.widget.Toaster;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.adapter.TKConfirmAdapter;
import com.jxwifi.cloud.quickclean.app.AccountMgr;
import com.jxwifi.cloud.quickclean.app.LocationGaoDe;
import com.jxwifi.cloud.quickclean.app.StateUtils;
import com.jxwifi.cloud.quickclean.app.UrlPool;
import com.jxwifi.cloud.quickclean.bean.InfoBean;
import com.jxwifi.cloud.quickclean.bean.PayParamsBean;
import com.jxwifi.cloud.quickclean.bean.ProductsBena;
import com.jxwifi.cloud.quickclean.model.AuzImpl;
import com.jxwifi.cloud.quickclean.okhttp.Params;
import com.jxwifi.cloud.quickclean.pay.PayResult;
import com.jxwifi.cloud.quickclean.pay.PayUtil;
import com.jxwifi.cloud.quickclean.popwindow.KTPopListModule;
import com.jxwifi.cloud.quickclean.popwindow.PopOrderStartModule;
import com.jxwifi.cloud.quickclean.popwindow.PopPayDepositModule;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;
import com.popwindow.PopWindowUtil;
import com.popwindow.Popwindows;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import cn.trinea.android.common.util.REPattern;

public class TKConfirmPresenter {

    private Context mContext;
    private TKConfirmView mView;
    private AuzImpl mAuz;

    private TKConfirmAdapter mAdapter;
    private String mAddressId;

    private String area;
    private String city;
    private String province;
    private String mAddressid; //地址id

    public TKConfirmPresenter(Context context, TKConfirmView view) {
        this.mContext = context;
        this.mView = view;
        mAuz = new AuzImpl();
    }

    public void initView() {

        mView.getConfirmName().setText(AccountMgr.getName(mContext));
        mView.getConfirmPhone().setText(AccountMgr.gettell(mContext));

        if (mView.getShopPur() != null) {
            mView.getTkConfirmPrice().setText("\u00A5" + mView.getShopPur().getDiscountSumVal());
            mView.getAllPrice().setText("\u00A5" + mView.getShopPur().getDiscountSumVal());
        }

        mView.getRvView().setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        mView.getRvView().setAdapter(mAdapter = new TKConfirmAdapter(R.layout.list_tk_confirm_items));
        mAdapter.openLoadAnimation();
        mAdapter.openLoadAnimation(BaseQuickAdapter.FOOTER_VIEW);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.img_confirm_items:
                        KTListPop(mView.getShopPur().getProducts());
                        break;
                }
            }
        });
        mAdapter.setNewData(mView.getShopPur().getProducts());
    }

    //用户信息 用户获取默认地址信息
    public void getinfo() {
        Params params = new Params();
        params.add("customerCode", AccountMgr.getCode(mContext));
        mAuz.auzget(UrlPool.GET_INFO, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                if (code == 0) {
                    InfoBean minfoBean = JSON.parseObject(data, InfoBean.class);
                    AccountMgr.putUser(mContext, minfoBean, true);
                    mView.onAddressBean(minfoBean.getDefaultAddress());
                    if (minfoBean.getDefaultAddress()!=null){
                        mAddressId=minfoBean.getDefaultAddress().getId();
                    }
                } else {
                    mView.oninfo();
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });

    }

    //工具包列表
    public void KTListPop(List<ProductsBena> mProducts) {
        KTPopListModule module = new KTPopListModule(mContext, mProducts);
        Popwindows window = PopWindowUtil.getPopupWindow(module.getView(), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup
                .LayoutParams.MATCH_PARENT);
        module.setPopwindow(window);
        window.show(mView.getTkAddress(), Popwindows.Location.TOP);
    }

    //支付布局
    public void paySetMeal() {
        if (TextUtils.isEmpty(mView.getConfirmName().getText().toString())) {
            Toaster.show(mContext, "联系人不能为空");
            return;
        }
        if (mView.getConfirmPhone().getText().toString().equals("")) {
            Toaster.show(mContext, "手机号码不能为空");
            return;
        }
        boolean isMobile = REPattern.isMobileNO(mView.getConfirmPhone().getText().toString());
        if (!isMobile) {
            Toaster.show(mContext, "请输入正确的手机号码");
            return;
        }

        if (TextUtils.isEmpty(mView.getTkAddress().getText().toString())){
            Toaster.show(mContext, "收货地址不能为空");
            return;
        }
        if (TextUtils.isEmpty(mView.getAddressDetails().getText().toString())){
            Toaster.show(mContext, "详细地址不能为空");
            return;
        }


        PopPayDepositModule mPopPayOrder = new PopPayDepositModule(mContext, Double.valueOf(mView.getShopPur().getDiscountSumVal()), "支付金额", new PopPayDepositModule.PopPayListener() {
            @Override
            public void getPayOrder(String PayType, String PayPrice) {
                onPrePay(PayType);
            }
        });
        Popwindows window = PopWindowUtil.getPopupWindow(mPopPayOrder.getView(), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup
                .LayoutParams.MATCH_PARENT);
        mPopPayOrder.setPopwindow(window);
        window.show(mView.getAddressDetails(), Popwindows.Location.TOP);
    }

    public void onPrePay(String PayType){
        Params params=new Params();
        params.add("addressId",mAddressId);
        params.add("orderCode",mView.getShopPur().getOrderCode());
        params.add("payChannelEnum",PayType);
        params.add("remark",mView.getRemarksText().getText().toString());
        params.add("tradeType","APP");
        params.add("userName",mView.getConfirmName().getText().toString());
        params.add("userTell",mView.getConfirmPhone().getText().toString());

        mAuz.auzpost(UrlPool.POST_SHOP_PAY_SHOP, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                PayParamsBean mParams = JSON.parseObject(data, PayParamsBean.class);
                if (mParams.isNoPayment()){
                    EventBus.getDefault().post(new PayResult(true, mParams.getOutTradNo()), "e-PayResult");
                }else{
                    if (StateUtils.PAY_ALI.equals(mParams.getPayChannelEnum())) { //支付宝
                        PayUtil.getInstance().aliPay(mContext, mParams);
                    } else if (StateUtils.PAY_WX.equals(mParams.getPayChannelEnum())) { //微信
                        mView.getWXPayCode(mParams.getOutTradNo());
                        PayUtil.getInstance().WXPay(mContext, mParams.getWxPayResult());
                    }
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }


}
